package com.zxw.auth.domain.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.zxw.common.core.base.entity.BaseEntity;
import com.zxw.common.core.base.entity.Role;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-02-08
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("ec_user")
public class User extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 账号
     */
    private String account;

    /**
     * 用户名称
     */
    private String username;

    /**
     * 密码
     */
    private String passwordCode;

    /**
     * 状态
     */
    private Boolean enabled;

    private List<Role> authoritiyList;

    private Collection<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        List<Role> roles = getAuthoritiyList();
        if (null == roles) {
            auths.add(new SimpleGrantedAuthority("visitor"));//游客
            auths.add(new SimpleGrantedAuthority("staff"));//职员
            return auths;
        }
        for (Role authorities : roles) {
            auths.add(new SimpleGrantedAuthority(authorities.getRoleName()));
        }
        return auths;
    }

    @Override
    public String getPassword() {
        return this.passwordCode;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {//是否锁定
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {//是否过期
        return true;
    }

    @Override
    public boolean isEnabled() {//是否禁用
        return true;
    }

    public User(String username, String password) {
        this.username = username;
        this.passwordCode = password;
    }
}
